//
//  NewViewController.m
//  项目A --半糖
//
//  Created by romana on 16/5/16.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "NewViewController.h"
#import "HeaderView.h"
#import "AFNetworking.h"
#import "NewVCModel.h"
#import "NewSecondModel.h"
#import "NewTableViewCell.h"
#import "NewSecondDetailsViewController.h"

static CGFloat const CELLH = 280;


#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height


@interface NewViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property(nonatomic,strong)NSMutableArray *myArrayTwo;


@end

@implementation NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myArray = [NSMutableArray array];
    
    [self getData];
    [self read];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49 - 44) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    //注册cell
    [self.myTableView registerClass:[NewTableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    //设置头视图
    HeaderView *headV = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 200)];
    self.myTableView.tableHeaderView = headV;
    

}

-(void)read{

    self.myArrayTwo = [NSMutableArray array];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *eventArrayPath = [path stringByAppendingPathComponent:@"eventArray.txt"];
    NSDictionary *dataDic = [NSDictionary dictionaryWithContentsOfFile:eventArrayPath];
    
    NewVCModel *newmodel = [[NewVCModel alloc] init];
    [newmodel setValuesForKeysWithDictionary:dataDic];
    for (NewSecondModel *model in newmodel.topic) {
        [self.myArrayTwo addObject:model];
    }

    


}

//取数据
-(void)getData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://open3.bantangapp.com/recommend/index?app_id=com.jzyd.BanTang&app_installtime=1434705307&app_versions=5.7&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&last_get_time=1463491553&os_versions=9.3.1&page=0&pagesize=20&screensize=1242&track_device_info=iPhone7%2C1&track_deviceid=C2D3B460-4E0E-4639-A030-D274CFC43D50&v=12%20HTTP/1.1" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
         NSMutableDictionary *bigDic = responseObject;
        NSMutableDictionary *dataDic = [bigDic objectForKey:@"data"];
        
        //缓存
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        NSString *eventArrayPath = [path stringByAppendingPathComponent:@"eventArray.txt"];
        //(2)将字典写入文件
        [dataDic writeToFile:eventArrayPath atomically:YES];
        
        NewVCModel *newmodel = [[NewVCModel alloc] init];
        [newmodel setValuesForKeysWithDictionary:dataDic];
        for (NewSecondModel *model in newmodel.topic) {
            [self.myArray addObject:model];
        }
        
        
      //  [self.myArray addObject:newmodel];
        
        
        [self.myTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];

    

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    
    if (self.myArray.count == 0) {
        return  self.myArrayTwo.count;
    } else {
        return self.myArray.count;
    }


}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
   //取消点击灰色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.myArray.count == 0) {
            cell.NewSModel = [self.myArrayTwo objectAtIndex:indexPath.row];
    } else {
            cell.NewSModel = [self.myArray objectAtIndex:indexPath.row];
    }


 //   cell.NewSModel = model;
    return cell;

  
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return CELLH;
}

//点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NewSecondDetailsViewController *newSeDeVC = [[NewSecondDetailsViewController alloc] init];
    newSeDeVC.newseModel = [self.myArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:newSeDeVC animated:YES];


}

@end
